package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Marco
 * @Description    定时任务
 * @date 2025/8/2 22:16
 * @email 3293336923@qq.com
 */
@Slf4j
@Component
public class ScheduleTasks {

    @Autowired
    private LeaseAgreementService  leaseAgreementService;

//    测试：每一秒就去执行一次
//    @Scheduled(cron = "* * * * * *")
//    public      void    test(){
//        log.info("当前时间:{}",new Date());
//    }


//   每天的零时零分去 查询数据库 ，
//   看如果有用户的 租约已到期，就将租约状态改为已到期
    @Scheduled(cron = "0 0 0 * * *") // 每年每月每日的 0 时 0 分 0 秒 执行
    public    void   checkLease(){
        LambdaUpdateWrapper<LeaseAgreement>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.le(LeaseAgreement::getLeaseEndDate , new Date());
        updateWrapper.in(LeaseAgreement::getStatus , LeaseStatus.SIGNED , LeaseStatus.WITHDRAWING);
        updateWrapper.set(LeaseAgreement::getStatus , LeaseStatus.EXPIRED);
        leaseAgreementService.update(updateWrapper);
    }
}
